/**
 * Created by Eran on 2015/11/17.
 */
package copyengine.resource.resProxy.cacheData
{
    import copyengine.log.CELog;
    import copyengine.resource.resProxy.CEResFIleDesc;

    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;
    import flash.system.System;
    import flash.utils.ByteArray;

    import starling.textures.TextureAtlas;

    public class CETextureAtlasResCacheData extends CETextureResCacheData
    {
        private var mAtlasXMLLoader:URLLoader;
        private var mAtlasXML:XML;

        private var mAtlasTexture:TextureAtlas;

        public function CETextureAtlasResCacheData() {
            super();
        }

        override protected function doLoadRes():void {
            loadAtlasConfigXml();
        }

        //============================================//
        //==Load XML
        //============================================//

        private function loadAtlasConfigXml():void {
            if (mFileDesc.isHaveExtValue(CEResFIleDesc.EXT_CACHE_BYTE_B)) {
                //尝试直接从本地缓存中load文件
                handleXMLByteArray(mFileDesc.getExtValue(CEResFIleDesc.EXT_CACHE_BYTE_B));
            }
            else {
                //从网上或者app目录以二进制形式load数据
                mAtlasXMLLoader = new URLLoader();
                mAtlasXMLLoader.dataFormat = URLLoaderDataFormat.BINARY;
                mAtlasXMLLoader.addEventListener(Event.COMPLETE, onAtlasConfigFileLoadComplete);
                mAtlasXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadInError);
                mAtlasXMLLoader.load(new URLRequest(mFileDesc.getExtValue(CEResFIleDesc.EXT_ATLAS_XML_FILE_PATH)));
            }
        }

        private function onFileLoadInError(e:Event):void {CELog.log("CETextureAtlasResCacheData->can't load atlas file path: " + mFileDesc.getExtValue(CEResFIleDesc.EXT_ATLAS_XML_FILE_PATH));}

        private function onAtlasConfigFileLoadComplete(e:Event):void {handleXMLByteArray(mAtlasXMLLoader.data);}

        private function handleXMLByteArray(_byteArray:ByteArray):void {
            mAtlasXML = new XML(_byteArray);
            //处理纹理
            startHandleTextureLoad();
        }

        //============================================//
        //==OverrideAble  Function
        //============================================//

        override protected function onTextureHandleComplete():void {
            if (mAtlasTexture == null) {
                mAtlasTexture = new TextureAtlas(mTexture, mAtlasXML);
                super.onTextureHandleComplete();
            }
        }

        override public function getData():* {return mAtlasTexture;}

        override protected function dispose():void {
            if (mLoadStatus == LOAD_STATUS_LOADED) {
                System.disposeXML(mAtlasXML);
                mAtlasXML = null;
                CELog.log("AtlasXML of : " + mFileDesc.fileName + " has been dispose");
            } else {
                CELog.err("Can not release AtlasXML data as the file : " + mFileDesc.fileName + " not been loaded yet");
            }

            super.dispose();
        }

    }
}
